AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

function ENT:SpawnFunction(ply, tr) -- Spawn function needed to make it appear on the spawn menu
	if (!tr.HitWorld) then return end
 
	local ent = ents.Create("carDealerNPC") -- Create the entity
	ent:SetPos(tr.HitPos + Vector(0, 0, 5)) -- Set it to spawn 50 units over the spot you aim at when spawning it
	ent:Spawn() -- Spawn it 
		
		return ent -- You need to return the entity to make it work
end
local carPos
function ENT:Initialize()
	carPos = self.Entity:GetPos() + self.Entity:GetAngles():Forward()*100 + Vector(0,0,60)

	util.AddNetworkString("startCarMenu")
	util.AddNetworkString("buyCar")
	util.AddNetworkString("attemptPickup")
	util.AddNetworkString("updateGarage")
	util.AddNetworkString("paintJobSwitch")

	self:SetModel("models/gman_high.mdl")
	self:SetHullType(HULL_HUMAN);
   	self:SetHullSizeNormal();
   	self:SetSolid( SOLID_BBOX )
   	self:SetMoveType( MOVETYPE_NONE )
	self:SetUseType( SIMPLE_USE )
	self:CapabilitiesAdd( CAP_ANIMATEDFACE || CAP_TURN_HEAD )
	self:DropToFloor()

	MakeNPCText(self.Entity, "Car Dealer")
end

function ENT:AcceptInput(input, activator, caller, data)
	activator:GetTable().OwnedCars = activator:GetTable().OwnedCars or {}
	net.Start("updateGarage")
		net.WriteTable(activator:GetTable().OwnedCars)
	net.Send(activator)

	net.Start("startCarMenu")
	net.Send(activator)
end

function attemptCarPurchase(length, ply)
	ply:GetTable().OwnedCars = ply:GetTable().OwnedCars or {}
	carName = net.ReadString()
	car = carSets[carName]

	if (tonumber(ply.DarkRPVars.money) >= car.Price) then

		table.insert(ply:GetTable().OwnedCars, carName)

		saveOwnedCars(ply)

		//PrintTable(ply:GetTable().OwnedCars)

		ply:AddMoney(-car.Price)

	elseif tonumber(ply.DarkRPVars.money) < car.Price then
		GAMEMODE:Notify(ply, 2, 4, "You can't afford a " .. carName)
	//elseif table.HasValue(ply:GetTable().OwnedCars, carName) then
	//	GAMEMODE:Notify(ply, 2, 4, "You already own a " .. carName)
	end
end
net.Receive("buyCar", attemptCarPurchase)

function attemptCarPickup(ply)
	ply:GetTable().CarsOut = ply:GetTable().CarsOut or {}
	carName = net.ReadString()
	car = carSets[carName]
	if table.HasValue(ply:GetTable().OwnedCars, carName) then
		local Vehicle
		for k,v in pairs(list.Get("Vehicles")) do
			if v.Name == carName then
				Vehicle = v
			end
		end

		local spawnentity = ents.Create(Vehicle.Class)
		spawnentity:SetModel(Vehicle.Model)

		local CarSpawnPoint
		local CarSpawnAngle

		if game.GetMap() == "rp_evocity_v2d" then
			CarSpawnPoint = Vector(4690, -3722, 120)
			CarSpawnAngle = Angle(0,-90,0)
		elseif game.GetMap() == "rp_mecklenburg_v2" then
			CarSpawnPoint = Vector(-628, 2415, -888)
			CarSpawnAngle = Angle(0,90,0)
		elseif game.GetMap() == "rp_townsend_v2" then
			CarSpawnPoint = Vector(1163, 7120, -195)
			CarSpawnAngle = Angle(0, 0, 0)
		elseif game.GetMap() == "rp_downtown_v4c" then
			CarSpawnPoint = Vector(-2676, -632, -204)
			CarSpawnAngle = Angle(0, -180, 0)
		elseif game.GetMap() == "rp_downtown_v4c_v2" then
			CarSpawnPoint = Vector(-2296, -646, -198)
			CarSpawnAngle = Angle(0, -180, 0)
		end


		
		spawnentity:SetPos(CarSpawnPoint)

		spawnentity.VehicleName = Vehicle.Name
		spawnentity.VehicleTable = Vehicle

		if Vehicle.KeyValues then
			for k, v in pairs(Vehicle.KeyValues) do
				spawnentity:SetKeyValue(k, v)
			end
		end

		/*if spawnentity:GetKeyValues().VehicleScript then
			spawnentity:SetKeyValue("VehicleScript", vscript)
		else
			spawnentity:SetKeyValue("vehiclescript", vscript)
		end*/

		spawnentity.ClassOverride = Vehicle.Class
		
		if Vehicle.Members then
			table.Merge(spawnentity, Vehicle.Members)
		end

		spawnentity:SetAngles(CarSpawnAngle)
		spawnentity:SetColor(paintJob)
		//spawnentity:SetMaterial("")
		spawnentity:Spawn()
		SetupCarSeats(ply, spawnentity)
		spawnentity:Activate()
		spawnentity:Own(ply)

		local CarSpawned

		for k,v in pairs(ply:GetTable().OwnedCars) do
			if v==carName then
				MsgAll("car removed")
				CarSpawned = k
			end
		end

		table.remove(ply:GetTable().OwnedCars, CarSpawned)

		saveOwnedCars(ply)

		net.Start("updateGarage")
			net.WriteTable(ply:GetTable().OwnedCars)
		net.Send(ply)
	end
end
net.Receive("attemptPickup", function(len, ply) attemptCarPickup(ply) end)

function switchPaintJob(length, ply)
	paintJob.r = tonumber(net.ReadString())
	paintJob.g = tonumber(net.ReadString())
	paintJob.b = tonumber(net.ReadString())
end
net.Receive("paintJobSwitch", switchPaintJob)

function clearPlayerVehicles(ply)
	ply:GetTable().OwnedCars = {}
end